package com.tz.crm.advice;

import com.tz.crm.dao.ErrorLogDao;
import com.tz.crm.entity.ErrorLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by jiamuhan on 2016/12/19.
 */
@Component
@Aspect
public class ErrorLogAdvice {

    @Autowired
    private ErrorLogDao errorLogDao;

    /**
     * Log error message
     * @param jp JoinPoint
     * @param e Throwable
     */
    @AfterThrowing(value = "execution(* com.tz.crm.controller.**.*Controller.*(..))",
            throwing = "e")
    public void errorLog(JoinPoint jp, Throwable e) {
        String className = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        String exName = e.getClass().getName();
        String exMessage = e.getMessage();
        ErrorLog errorLog = new ErrorLog();
        errorLog.setClassName(className);
        errorLog.setMethodName(methodName);
        errorLog.setExName(exName);
        errorLog.setExMessage(exMessage);
        errorLogDao.save(errorLog);
    }
}
